package cn.always.xiajia.admin.sys.service.impl;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Service;

import cn.always.xiajia.admin.sys.entity.SysUserDept;
import cn.always.xiajia.admin.sys.mapper.SysUserDeptMapper;
import cn.always.xiajia.admin.sys.service.SysUserDeptService;
import cn.always.xiajia.framework.common.util.ExtCollUtil;
import cn.always.xiajia.framework.common.util.ExtCollectionUtils;
import cn.always.xiajia.framework.mybatis.core.service.impl.BaseServiceImplX;

/**
 * 
 * @author xgj
 *
 */
@Service("sysUserDeptService")
public class SysUserDeptServiceImpl extends BaseServiceImplX<SysUserDeptMapper, SysUserDept> implements SysUserDeptService {

	/**
	 * 根据用户获取对应部门Id
	 * 
	 * @param userId
	 * @return
	 */
	@Override
	public Set<Long> getDeptIdByUserId(Long userId) {
		if (userId == null) {
			return Collections.emptySet();
		}
		Map<String, Object> param = new HashMap<>();
		param.put("EQ_userId", userId);
		return ExtCollectionUtils.convertSet(xjList(param), SysUserDept::getDeptId);
	}

	/**
	 * 获得拥有部门的用户编号集合
	 *
	 * @param deptId 部门编号
	 * @return 用户编号集合
	 */
	@Override
	public Set<Long> getUserIdByDeptId(Long deptId) {
		return getUserIdByDeptIds(Collections.singletonList(deptId));
	}

	/**
	 * 获得拥有多个部门的用户编号集合
	 *
	 * @param deptIds 部门编号集合
	 * @return 用户编号集合
	 */
	@Override
	public Set<Long> getUserIdByDeptIds(Collection<Long> deptIds) {
		if (ExtCollUtil.isEmpty(deptIds)) {
			return Collections.emptySet();
		}
		Map<String, Object> param = new HashMap<>();
		param.put("IN_deptId", deptIds);
		return ExtCollectionUtils.convertSet(xjList(param), SysUserDept::getDeptId);
	}
}
